package com.colud.client.test;

import com.colud.client.util.Constants;
import com.colud.client.util.JT809Constants;
import com.colud.client.vo.Message;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Component;

import static com.colud.client.util.ClientUtils.getBytesWithLengthAfter;

/**
 * @author mfy
 * @date 2021/10/13
 * @DESC 业务处理
 */
@Component
@EnableAutoConfiguration
public class JT809Manager {

    private static Logger log = LoggerFactory.getLogger(JT809Manager.class);

    /**
     * 交委指定本公司接入码
     */
    public static int PLANT_CODE = 53079;

    /**
     * 登录状态
     */
    public static String LoginStatus = "";

    /**
     * 登陆中
     */
    public static String Logining = "success";

    /**
     * 登录标志
     */
    private static int LOGIN_FLAG = 0;

    //初始化通道
    private Channel channel = new JT809Client().getChannel(Constants.SERVER_IP, Constants.SERVER_PORT);

    /**
     * 登录交委接入平台
     * boolean
     */
    public boolean login2Gov() {

        boolean success = false;
        if (!Constants.LOGIN_SUCCESS.equals(LoginStatus)) {
            //消息类型  登录请求
            Message msg = new Message(JT809Constants.UP_CONNECT_REQ);
            ChannelBuffer buffer = ChannelBuffers.buffer(46);

            //buffer.writeInt(jt809LoginConfig.getCOM_ID());//4
            buffer.writeInt(Constants.COM_ID);//4

            //byte[] pwd = getBytesWithLengthAfter(8, jt809LoginConfig.getCOM_PWD().getBytes());
            byte[] pwd = getBytesWithLengthAfter(8, Constants.COM_PWD.getBytes());
            buffer.writeBytes(pwd);//8

            //byte[] ip = getBytesWithLengthAfter(32, jt809Config.getDOWN_LINK_IP().getBytes());
            byte[] ip = getBytesWithLengthAfter(32, Constants.DOWN_LINK_IP.getBytes());
            buffer.writeBytes(ip);//32

            //buffer.writeShort((short) jt809Config.getDOWN_LINK_PORT());//2
            buffer.writeShort((short) Constants.DOWN_LINK_PORT);//2
            /** 数据体*/
            msg.setMsgBody(buffer);
            System.out.println(msg);
            channel.write(msg);
            LoginStatus = Logining;
            success = true;
        }
        return success;
    }

    /**
     * 发送业务数据到交委上级接入平台  下级发送到上级
     *
     * @param msg
     */
    public void sendMsg(Message msg) {

        if (LoginStatus.equals(Constants.LOGIN_SUCCESS)) {
            //已经登录成功，开始发送数据
            if (null != channel && channel.isWritable()) {
                channel.write(msg);
                log.info("send-----------" + msg.toString());
            } else {
                LoginStatus = "";
            }
        } else {
            //登录上级平台
            login2Gov();
            log.info("------------first login");
        }
    }

    /**
     * 发送应答到交委上级接入平台
     *
     * @param msg
     */
    public void reqMsg(Message msg) {
        if (LoginStatus.equals(Constants.LOGIN_SUCCESS)) {
            //已经登录成功，开始发送数据
            if (null != channel && channel.isWritable()) {
                channel.write(msg);
                log.info("send---------" + msg.toString());
            } else {
                LoginStatus = "";
            }
        } else {
            //登录上级平台
            login2Gov();
            log.info("------------first login");
        }
    }

}
